<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="/common.jsp" %>
<!DOCTYPE html>
<html lang="zh">
<meta charset="utf-8">
<head
"></head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-user-edit">
        <input name="userId" type="hidden" value="${user.userId}"/>
        <input name="deptId" type="hidden" value="${user.deptId}" id="treeId"/>
        <div class="form-group">
            <label class="col-sm-3 control-label ">登录名称：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" readonly="true" value="${user.loginName}"/>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">部门名称：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="deptName" onclick="selectDeptTree()" readonly="true"
                       id="treeName" value="${user.dept.deptName}"/>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">用户名称：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="userName" id="userName"
                       value="${user.userName}"/>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">邮箱：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="email" id="email" value="${user.email}"/>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">手机：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="phonenumber" id="phonenumber"
                       value="${user.phonenumber}"/>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">性别：</label>
            <div class="col-sm-8">
                <select id="sex" class="form-control m-b">
                    <option value="1" ${user.sex=='1'?'selected':''}>男性</option>
                    <option value="0" ${user.sex=='0'?'selected':''}>女性</option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">状态：</label>
            <div class="col-sm-8">
                <div class="onoffswitch">
                    <input type="checkbox"  ${user.status == '0' ? 'checked' : ''}
                           class="onoffswitch-checkbox" id="status" name="status">
                    <label class="onoffswitch-label" for="status">
                        <span class="onoffswitch-inner"></span>
                        <span class="onoffswitch-switch"></span>
                    </label>
                </div>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">角色：</label>
            <div class="col-sm-8">
                <label class="check-box">
                    <c:forEach items="${roles}" var="role">
                        <input name="role" type="checkbox" value="${role.roleId}" ${role.flag ?'checked':''}
                               style="margin-left: 10px;"/>&nbsp;
                        <label>${role.roleName}</label>
                    </c:forEach>
                </label>
            </div>
        </div>
    </form>
</div>
<div th:include="include::footer"></div>
<script th:src="@{/ajax/libs/select/select2.js}"></script>
<script>
    $("#form-user-edit").validate({
        rules: {
            userName: {
                required: true,
            },
            deptName: {
                required: true,
            },
            email: {
                required: true,
                email: true,
                remote: {
                    url: "${ctx}/system/user/checkEmailUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "userId": function () {
                            return $("#userId").val();
                        },
                        "email": function () {
                            return $.common.trim($("#email").val());
                        }
                    },
                    dataFilter: function (data, type) {
                        return $.validate.unique(data);
                    }
                }
            },
            phonenumber: {
                required: true,
                isPhone: true,
                remote: {
                    url: "${ctx}/system/user/checkPhoneUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "userId": function () {
                            return $("#userId").val();
                        },
                        "phonenumber": function () {
                            return $.common.trim($("#phonenumber").val());
                        }
                    },
                    dataFilter: function (data, type) {
                        return $.validate.unique(data);
                    }
                }
            },
        },
        messages: {
            "email": {
                remote: "Email已经存在"
            },
            "phonenumber": {
                remote: "手机号码已经存在"
            }
        },
        submitHandler: function (form) {
            edit();
        }
    });

    function submitHandler() {
        if ($.validate.form()) {
            edit();
        }
    }

    function edit() {
        var userId = $("input[name='userId']").val();
        var deptId = $("input[name='deptId']").val();
        var userName = $("input[name='userName']").val();
        var email = $("input[name='email']").val();
        var phonenumber = $("input[name='phonenumber']").val();
        var sex = $("#sex option:selected").val();
        var status = $("input[name='status']").is(':checked') == true ? 0 : 1;
        var roleIds = $.form.selectCheckeds("role");
        var postIds = $.form.selectSelects("post");
        $.ajax({
            cache: true,
            type: "POST",
            url: "${ctx}/system/user/edit",
            data: {
                "userId": userId,
                "deptId": deptId,
                "userName": userName,
                "email": email,
                "phonenumber": phonenumber,
                "sex": sex,
                "status": status,
                "roleIds": roleIds,
                "postIds": postIds
            },
            async: false,
            error: function (request) {
                $.modal.alertError("系统错误");
            },
            success: function (data) {
                $.operate.successCallback(data);
            }
        });
    }

    /*用户管理-修改-选择部门树*/
    function selectDeptTree() {
        var deptId = $.common.isEmpty($("#treeId").val()) ? "100" : $("#treeId").val();
        var url = "${ctx}/system/dept/selectDeptTree/" + deptId;
        var options = {
            title: '选择部门',
            width: "380",
            url: url,
            callBack: doSubmit
        };
        $.modal.openOptions(options);
    }

    function doSubmit(index, layero) {
        var tree = layero.find("iframe")[0].contentWindow.$._tree;
        if ($.tree.notAllowParents(tree)) {
            var body = layer.getChildFrame('body', index);
            $("#treeId").val(body.find('#treeId').val());
            $("#treeName").val(body.find('#treeName').val());
            layer.close(index);
        }
    }
</script>
</body>
</html>
